SQL 您所在的位置:网站首页 sql 100题 SQL

SQL

2023-10-07 20:26| 来源: 网络整理| 查看: 265

点击上方“咸鱼学Python”,选择“加为星标”

第一时间关注Python技术干货!

作者:tomocat

来源:知乎

00 相关推荐

SQL | 数据分析面试必备SQL语句+语法

SQL | 开发人员必学的几点 SQL 优化点

接下来是是关于44道经典SQL测试题

01 建表语句

create table Student(sid varchar(10),sname varchar(10),sage datetime,ssex nvarchar(10)); insert into Student values('01' , '赵雷' , '1990-01-01' , '男'); insert into Student values('02' , '钱电' , '1990-12-21' , '男'); insert into Student values('03' , '孙风' , '1990-05-20' , '男'); insert into Student values('04' , '李云' , '1990-08-06' , '男'); insert into Student values('05' , '周梅' , '1991-12-01' , '女'); insert into Student values('06' , '吴兰' , '1992-03-01' , '女'); insert into Student values('07' , '郑竹' , '1989-07-01' , '女'); insert into Student values('08' , '王菊' , '1990-01-20' , '女'); create table Course(cid varchar(10),cname varchar(10),tid varchar(10)); insert into Course values('01' , '语文' , '02'); insert into Course values('02' , '数学' , '01'); insert into Course values('03' , '英语' , '03'); create table Teacher(tid varchar(10),tname varchar(10)); insert into Teacher values('01' , '张三'); insert into Teacher values('02' , '李四'); insert into Teacher values('03' , '王五'); create table SC(sid varchar(10),cid varchar(10),score decimal(18,1)); insert into SC values('01' , '01' , 80); insert into SC values('01' , '02' , 90); insert into SC values('01' , '03' , 99); insert into SC values('02' , '01' , 70); insert into SC values('02' , '02' , 60); insert into SC values('02' , '03' , 80); insert into SC values('03' , '01' , 80); insert into SC values('03' , '02' , 80); insert into SC values('03' , '03' , 80); insert into SC values('04' , '01' , 50); insert into SC values('04' , '02' , 30); insert into SC values('04' , '03' , 20); insert into SC values('05' , '01' , 76); insert into SC values('05' , '02' , 87); insert into SC values('06' , '01' , 31); insert into SC values('06' , '03' , 34); insert into SC values('07' , '02' , 89); insert into SC values('07' , '03' , 98);

02 表结构预览

--学生表 Student(SId,Sname,Sage,Ssex) --SId 学生编号,Sname 学生姓名,Sage 出生年月,Ssex 学生性别 --课程表 Course(CId,Cname,TId) --CId 课程编号,Cname 课程名称,TId 教师编号 --教师表 Teacher(TId,Tname) --TId 教师编号,Tname 教师姓名 --成绩表 SC(SId,CId,score) --SId 学生编号,CId 课程编号,score 分数

1. 查询“01”课程比“02”课程成绩高的所有学生的学号;

select distinct t1.sid as sidfrom (select * from sc where cid='01')t1 left join (select * from sc where cid='02')t2 on t1.sid=t2.sid where t1.score>t2.score

2. 查询平均成绩大于60分的同学的学号和平均成绩;

select sid ,avg(score) from sc group by sid having avg(score)>60

3. 查询所有同学的学号、姓名、选课数、总成绩

select student.sid as sid ,sname ,count(distinct cid) course_cnt ,sum(score) as total_score from student left join sc on student.sid=sc.sid group by sid,sname

4. 查询姓“李”的老师的个数;

select count(distinct tid) as teacher_cnt from teacher where tname like '李%'

5. 查询没学过“张三”老师课的同学的学号、姓名;

select sid,sname from student where sid not in ( select sc.sid from teacher left join course on teacher.tid=course.tid left join sc on course.cid=sc.cid where teacher.tname='张三' )

6. 查询学过“01”并且也学过编号“02”课程的同学的学号、姓名;

select t.sid as sid ,sname from ( select sid ,count(if(cid='01',score,null)) as count1 ,count(if(cid='02',score,null)) as count2 from sc group by sid having count(if(cid='01',score,null))>0 and count(if(cid='02',score,null))>0 )t left join student on t.sid=student.sid

7. 查询学过“张三”老师所教的课的同学的学号、姓名;

select student.sid ,sname from ( select distinct cid from course left join teacher on course.tid=teacher.tid where teacher.tname='张三' )course left join sc on course.cid=sc.cid left join student on sc.sid=student.sid group by student.sid,sname

8. 查询课程编号“01”的成绩比课程编号“02”课程低的所有同学的学号、姓名;

select t1.sid,sname from ( select distinct t1.sid as sid from (select * from sc where cid='01')t1 left join (select * from sc where cid='02')t2 on t1.sid=t2.sid where t1.score>t2.score )t1 left join student on t1.sid=student.sid

9. 查询所有课程成绩小于60分的同学的学号、姓名;

select t1.sid,sname from ( select sid,max(score) from sc group by sid having max(score=course.cid left join teacher style="font-weight: 600;">=teacher.tid left join student style="font-weight: 600;">=student.sid where tname='张三' order by score desc limit 1;

36. 查询每门功课成绩最好的前两名

select cid,sid,rank1 from ( select cid ,sid ,rank() over(partition by cid order by score desc) as rank1 from sc )t where rank1 =5 order by count(sid) desc,cid

38. 检索至少选修两门课程的学生学号

select sid ,count(cid) from sc group by sid having count(cid)>=2

39. 查询选修了全部课程的学生信息

select sid ,count(cid) from sc group by sid having count(cid)=(select count(distinct cid) from sc)

40. 查询各学生的年龄

select sid,sname,year(curdate())-year(sage) as sage from student

41 查询本周过生日的学生

select sid,sname,sage from student where weekofyear(sage)=weekofyear(curdate())

42. 查询下周过生日的学生

select sid,sname,sage from student where weekofyear(sage) = weekofyear(date_add(curdate(),interval 1 week))

43 查询本月过生日的学生

select sid,sname,sage from student where month(sage) = month(curdate())

44. 查询下月过生日的学生

select sid,sname,sage from student where month(date_sub(sage,interval 1 month)) = month(curdate())

Love & Share

[ 完 ]

朕已阅



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有